package cn.virens.web.controller.manage.platform;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.virens.common.RequestUtil;
import cn.virens.exception.APIException;
import cn.virens.service.impl.platform.generator.GeneratorDatabaseTemplate;
import cn.virens.service.impl.platform.generator.common.ColumnInfo;
import cn.virens.service.impl.platform.generator.common.TableInfoEx;
import cn.virens.service.inter.platform.generator.GeneratorDatabaseService;
import cn.virens.web.common.BaseUserController;

@Controller
@RequestMapping("/manage/platform/generator/database")
public class GeneratorDatabaseContorller extends BaseUserController {
	private @Autowired GeneratorDatabaseTemplate mPlatformTemplate;
	private @Autowired GeneratorDatabaseService mPlatformService;

	public GeneratorDatabaseContorller() {
		super("/manage/platform/generator/database");
	}

	@RequestMapping("index.jspx")
	public String indexJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		return returnUrl("index");
	}

	@RequestMapping("table.jspx")
	public String tableJspx(@RequestParam("name") String name, @RequestParam("table") String table, Model model) {
		model.addAttribute("controlName", StrUtil.upperFirst(StrUtil.toCamelCase(table)));
		model.addAttribute("controlMapping", StrUtil.replace(table, "_", "/"));
		model.addAttribute("modelPackage", StrUtil.replace(table, "_", "."));
		model.addAttribute("modelTable", table);
		model.addAttribute("name", name);

		return returnUrl("table");
	}

	@ResponseBody
	@RequestMapping("table.json")
	public Model table(HttpServletRequest request, HttpServletResponse response, Model model) throws APIException {
		List<Map<String, Object>> list = mPlatformService.showTables();

		model.addAttribute("count", list.size());
		model.addAttribute("data", list);
		model.addAttribute("code", "0");
		model.addAttribute("msg", "");

		return model;
	}

	@ResponseBody
	@RequestMapping("columns.json")
	public Model columns(HttpServletRequest request, HttpServletResponse response, Model model) throws APIException {
		List<Map<String, Object>> list = mPlatformService.showColumns(RequestUtil.getValueStr(request, "table"));

		model.addAttribute("count", list.size());
		model.addAttribute("data", list);
		model.addAttribute("code", "0");
		model.addAttribute("msg", "");

		return model;
	}

	@ResponseBody
	@RequestMapping("build.json")
	public Model build(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response, Model model) throws APIException {
		TableInfoEx info = mPlatformTemplate.info();
		info.setControlMapping(json.getString("controlMapping"));
		info.setModelPackage(json.getString("modelPackage"));
		info.setControlName(json.getString("controlName"));
		info.setModelTable(json.getString("modelTable"));
		info.setName(json.getString("name"));
		info.setDate(DateUtil.today());

		// 填充字段信息
		Map<String, Object> map = new HashMap<>();
		map.put("viewList", toJavaList(json, "viewList", ColumnInfo.class));
		map.put("indexList", toJavaList(json, "indexList", ColumnInfo.class));
		map.put("createList", toJavaList(json, "createList", ColumnInfo.class));
		map.put("updateList", toJavaList(json, "updateList", ColumnInfo.class));

		// 生成返回数据
		Map<String, String> result = new HashMap<>();
		// 基础代码渲染
		result.put("control", mPlatformTemplate.renderControl(info, map));
		result.put("service", mPlatformTemplate.renderService(info, map));
		result.put("serviceImpl", mPlatformTemplate.renderServiceImpl(info, map));

		// HTML页面渲染
		if (map.containsKey("viewList") && map.get("viewList") != null) {
			result.put("viewList", mPlatformTemplate.renderPageView(info, map));
		}
		if (map.containsKey("indexList") && map.get("indexList") != null) {
			result.put("indexList", mPlatformTemplate.renderPageIndex(info, map));
		}
		if (map.containsKey("createList") && map.get("createList") != null) {
			result.put("createList", mPlatformTemplate.renderPageCreate(info, map));
		}
		if (map.containsKey("updateList") && map.get("updateList") != null) {
			result.put("updateList", mPlatformTemplate.renderPageUpdate(info, map));
		}

		return renderSuccess(result, model);
	}

	private static <T> List<T> toJavaList(JSONObject json, String key, Class<T> clazz) {
		JSONArray array = json.getJSONArray(key);

		if (array != null && !array.isEmpty()) {
			return array.toJavaList(clazz);
		} else {
			return null;
		}
	}
}
